

//添加内容
var addhtml=''
addhtml+='<div class="form-div"><form id="fomr" class="layui-form">';
addhtml+='<div class="layui-form-item"><label class="layui-form-label">用户组名</label><div class="layui-input-block"><input type="text" name="group_name" autocomplete="off" class="layui-input" placeholder="请输入用户组名"></div></div>'
addhtml+='<div class="layui-form-item" id="inputCheckbox"><label class="layui-form-label">用户组权限</label></div>'
addhtml+='<input type="hidden" name="token" value="'+token+'">';
addhtml+='</form></div>';


//编辑内容
var edithtml=''
edithtml+='<div class="form-div"><form id="fomr" class="layui-form">';
edithtml+='<div class="layui-form-item"><label class="layui-form-label">用户组名</label><div class="layui-input-block"><input type="text" name="group_name" autocomplete="off" class="layui-input" placeholder="请输入用户组名"></div></div>'
edithtml+='<div class="layui-form-item" id="inputCheckbox"><label class="layui-form-label">用户组权限</label></div>'
edithtml+='<input type="hidden" name="token" value="'+token+'">';
edithtml+='<input type="hidden" name="id">';
edithtml+='</form></div>';

layui.use('table', function() {
	var table = layui.table,form = layui.form;
	
	layer.msg('数据加载中...', {
		icon: 16,
		shade: 0.3,
		success: function() {
			table.render({
				elem: '#centet-cen',
				url: listSrc,
				height: $(".center-tab").height(),
				id:'idTest',
				response: {
		  			statusCode: 1, //成功的状态码，默认：0
				},   
				page:false,
				cols: [
					[ //标题栏
						{
							field: 'id',
							title: 'ID',
							sort: true
						}, {
							field: 'group_name',
							title: '用户组名称'
						}, {
							field: 'oper',
							title: '操作',
							width: 300,
							minWidth: 160,
							toolbar: '#barDemo'
						}

					]
				],
				limit: 10
			});
			layer.close(layer.index);
		}
		
		
	})
	
	table.on('tool(table)', function(obj) {
		
		var data = obj.data;
		if(obj.event === 'edit') {
			var id = data.id;
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: seeSrc,
				data: {
					id: id
				},
				success: function(data) {
					layer.open({
						title: "编辑管理员",
						type: 1,
						skin: 'layui-layer-rim',
						area: ['550px', '450px'],
						content: edithtml,
						btn: ['提交', '关闭'],
						btn1: function() {
							var Adata = $("#fomr").serialize();
							$.ajax({
								type: 'post',
								dataType: 'json',
								url: editSrc,
								data: Adata,
								success: function(data) {
									var code = data.code;
									var msg = data.msg;
									if(code == 1) {
										layer.close(layer.index)
										layer.alert(msg, {
											icon: 1
										}, function() {
											location.reload();
										})
									} else {
										layer.alert(msg, {
											icon: 5
										})
									}
								}
							})
						}
					});
					$("#inputCheckbox").append($("#gllisthtml").html());
					$("#fomr").formEdit(data.data);
					form.render();
				}
			})
			
			
			
		}else if(obj.event === 'del'){
			var id = data.id;
			layer.alert('您确定要删除该管理员！',{
				icon: 2,
				btn: ['确定', '关闭'],
				btn1: function() {
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: delSrc,
						data: {
							token: token,
							id: id
						},
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			})
		}
		
		
	})
})







layui.use(['form','layer'], function() {
	var form = layui.form,layer = layui.layer;
	 function authes(){
    	$.ajax({
     		type:'post',
     		dataType:'json',
     		url:gllistSrc,
     		success:function(data){
     			if(data.code==1){
     				$.each(data.data,function(index,value){
     					var html = '<div class="layui-input-block" id="'+index+'"></div><hr>';
     					$('#gllisthtml').append(html);
     					$.each(value, function(i,v) {
     						var ckhtml = '<input type="checkbox" class="layui-form" name="group_auth[]" value="'+v.id+'" lay-skin="primary" title="'+v.auth_name+'-'+v.type_name+'">';
     						var Cen=$("#gllisthtml .layui-input-block")
     						$.each(Cen,function(j,k){
     							if($(this).attr("id")==index){
     								$(this).append(ckhtml);
     								form.render();
     							}
     						})
     					   
     					});
     					
     				})
     				
     			}
     		}
     	})
   }
	authes();
	var active = {
		add: function(){
			  layer.open({
			  type: 1,
			  title:"添加用户组",
			  area: ['70%', '70%'], //宽高
			  maxmin: true,
			  resize:true,
			  content: addhtml,
			  btn: ['提交', '关闭'],
			  btn1: function(){
			  	var Adata=$("#fomr").serialize();
			    $.ajax({
			    	type:'post',
			    	dataType:'json',
			    	url:addSrc,
			    	data:Adata,
			    	success:function(data){
			    		var code=data.code;
			    		var msg=data.msg;
			    		if(code==1){
			    			layer.close(layer.index)
			    			layer.alert(msg,{icon: 1},function(){
			    				location.reload();
			    			})
			    		}else{
			    			layer.alert(msg,{icon: 5})
			    		}
			    	}
			    })
			  }
			  
			});
			$("#inputCheckbox").append($("#gllisthtml").html());
			form.render();
			
		}
	}
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});
})


